import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from urllib.parse import urlencode
from commit_func import get_token
from aes_decrypt import decrypt_aes
from langchain_core.tools import tool
import requests



@tool
def get_dong_yang_kq(user_id, begindate, end_date):
    """查询工号(user_id)在(begindate, end_date)之间的考勤数据，时间格式按照YYYY-MM-DD"""
    base_url = "http://192.168.22.92:8080/portal/api/emphelp/getDailyAttendReports"

    header = {
        "token": get_token(user_id),
    }

    # begindate = "2021-06-01"
    # end_date = "2021-06-30"

    params = {
        "begindate": begindate,
        "enddate": end_date,
    }

    # query = {
    #     "pageindex": 1,
    #     "pagenum": 10,
    #     "pk_group": "0001A210000000000P86"
    # }

    query_string = urlencode(params)
    url = f"{base_url}?{query_string}"

    response = requests.get(url, headers=header)
    # res = decrypt_aes(response.text)
    result = {response.json()['data'][0].get('empNO'): response.json()['data'][0].get('empName')}
    for item in response.json()['data']:
        if item.get('attendType') == "":
            result[item.get('empDate')] = {"排班":item.get('shift'), "考勤情况":"正常班"}
        else:
            result[item.get('empDate')] = {"排班":item.get('shift'), "考勤情况":item.get('attendType'), "timeSession":item.get('timeSession')}
        # 将当前日期的考勤数据添加到对应的empNo下
    return result


if __name__ == "__main__":
    print(get_dong_yang_kq('040267', '2021-06-01', '2021-06-30'))
